package cn.edu.hnnu.c.rentacar.dao.hibernateimpl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import cn.edu.hnnu.c.rentacar.beans.User;
import cn.edu.hnnu.c.rentacar.idao.IUserDAO;
import cn.edu.hnnu.c.rentacar.util.HibernateUtil;

public class UserDAOHibernateImpl implements IUserDAO {
	private static UserDAOHibernateImpl instance;

	private UserDAOHibernateImpl() {

	}

	public static UserDAOHibernateImpl getInstance() {
		if (null == instance) {
			instance = new UserDAOHibernateImpl();
		}
		return instance;
	}

	@Override
	public boolean addUser(User user) {
		SessionFactory factory = null;
		Session session = null;
		Transaction tran = null;

		try {
			factory = HibernateUtil.getSessionFactory();
			session = factory.openSession();
			tran = session.beginTransaction();
			session.save(user);
			tran.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tran.rollback();
			return false;
		} finally {
			session.close();
		}
		return true;
	}

	@Override
	public boolean deleteUserById(int userId) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public User getUserById(int userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> getAllUsers() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean updateUer() {
		// TODO Auto-generated method stub
		return false;
	}

}
